package com.jjc.android.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.activity.CameraActivity
import com.jjc.android.activity.DetectionActivity
import com.jjc.android.activity.DetectionReportActivity
import com.jjc.android.base.BaseActivity
import com.jjc.android.base.BaseLazyLoadFragment
import com.jjc.android.dialog.MessageImageDialog
import com.jjc.android.entity.DetectionInfo
import com.jjc.android.util.AndroidBug5497Workaround
import com.jjc.android.util.messenger.ActivityMessenger
import com.jjc.android.view.layout.onClick
import com.jjc.android.view.onClick

class SamplingTakePhotoActivity : BaseActivity() {

    companion object{
        fun start(context: Activity, detectionInfo: DetectionInfo, action:(Boolean)->Unit){
            val intent = Intent(context,SamplingTakePhotoActivity::class.java)
            intent.putExtra(Constants.BUNDLE_INFO,detectionInfo)
            ActivityMessenger.startActivityForResult(context,intent){
                (it?.getBooleanExtra(Constants.BUNDLE_INFO,false)==true).let(action)
            }
        }
    }

    private val failerLayout: LinearLayout by lazy { findViewById(R.id.failerLayout) }
    private val reTakeBtn: TextView by lazy { findViewById(R.id.reTakeBtn) }
    private val toTakeLayout: LinearLayout by lazy { findViewById(R.id.toTakeLayout) }
    private val okBtn: TextView by lazy { findViewById(R.id.okBtn) }
    private val takePhotoImageView: ImageView by lazy { findViewById(R.id.takePhotoImageView) }
    private val homeBtn: ImageView by lazy { findViewById<ImageView>(R.id.homeBtn) }
    override val layoutId: Int = R.layout.activity_sampling_take_photo
    private lateinit var detectionInfo: DetectionInfo
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.prepare_take_photo_txt))
        AndroidBug5497Workaround.assistActivity(this)
        homeBtn.visibility = View.VISIBLE
        homeBtn.onClick {
            MessageImageDialog.Builder(this)
                .imageResId(R.mipmap.alert_jiancejindu_pic)
                .content(getString(R.string.dete_not_save_txt))
                .okClick {
                    MainActivity.start(this)
                }
                .build()
                .show()
        }
        showTakeLayout()
        val action:()->Unit = {
            showTakeLayout()
            CameraActivity.start(this,detectionInfo){detectionId->
                if(detectionId.isNullOrEmpty()){
                    showFailureLayout()
                }else{
                    DetectionReportActivity.start(this,detectionId)
                    setResult(Activity.RESULT_OK, Intent().apply {
                        putExtra(Constants.BUNDLE_INFO,true)
                    })
                    finish()
                }
            }
        }
        reTakeBtn.onClick(action = action)
        okBtn.onClick (action = action)
    }

    private fun showFailureLayout(){
        toTakeLayout.visibility = View.GONE
        failerLayout.visibility = View.VISIBLE
        playGif(false)
    }

    private fun showTakeLayout(){
        toTakeLayout.visibility = View.VISIBLE
        failerLayout.visibility = View.GONE
        playGif(true)
    }

    private fun playGif(isPlay:Boolean){
        if(isPlay){
            Glide
                .with(this)
                .asGif()
                .load(R.mipmap.insert_card_gif_icon)
                .into(takePhotoImageView)
        }else{
            if (takePhotoImageView.drawable is GifDrawable) {
                val drawable = takePhotoImageView.drawable as GifDrawable
                if (drawable.isRunning) {
                    drawable.stop()
                }
            }
        }
    }

    override fun setupArguments(args: Bundle?) {
        detectionInfo = (args?.getSerializable(Constants.BUNDLE_INFO) as? DetectionInfo)?:DetectionInfo()
    }

}